package com.woniuxy.signing.app.controller;

import java.time.LocalDateTime;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.I2F;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.signing.client.CocahSigningVenueClient;
import com.woniuxy.signing.client.StudentSigningClient;
import com.woniuxy.signing.client.form.MakeSureSignForm;
import com.woniuxy.signing.common.SigningException;
import com.woniuxy.signing.common.SigningResultCode;
import com.woniuxy.signing.infra.model.CocahSigningVenue;
import com.woniuxy.signing.infra.model.StudentSigningCocah;
import com.woniuxy.signing.service.CocahSigningVenueService;
import com.woniuxy.signing.service.StudentSigningCocahService;
import com.woniuxy.userapp.client.CoachClient;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huanglei
 * @since 2022-08-07
 */
@RestController
public class StudentSigningCocahController implements StudentSigningClient {
    @Resource
    private CoachClient coachClient;
    @Resource
    private CocahSigningVenueService cocahSigningVenueService;

    @Resource
    private StudentSigningCocahService studentSigningCocahService;

    @ApiOperation("当前登录学员id跟教练签约")
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public Result studentSigningCocah(String coachId, String loginStudentId) {

//        try {
        //1.教练存不存在
        Result<Integer> accountIdByCoachId = coachClient.findAccountIdByCoachId(Integer.parseInt(coachId.trim()));
        if (accountIdByCoachId.getData() == null) {
            throw new SigningException(SigningResultCode.COACH_NOTEXIST.getCode(), SigningResultCode.COACH_NOTEXIST.getMessage());
        }
        //2.教练是否签约场馆
        List<CocahSigningVenue> listDB = cocahSigningVenueService.list(new QueryWrapper<CocahSigningVenue>().eq("cocah_id", coachId));
        if (listDB == null || listDB.size() == 0) {
            throw new SigningException(SigningResultCode.COACH_NOTSIGN.getCode(), SigningResultCode.COACH_NOTSIGN.getMessage());
        }

        //3.是否以及和教练签约
        StudentSigningCocah studentSigningCocahDB = studentSigningCocahService.getOne(new QueryWrapper<StudentSigningCocah>().eq("sign_coach_id", coachId).eq("sign_student_id", loginStudentId));
        if (studentSigningCocahDB != null) {
            throw new SigningException(SigningResultCode.COACH_ISSIGNMENT.getCode(), SigningResultCode.COACH_ISSIGNMENT.getMessage());
        }
        //4.签约
        StudentSigningCocah studentSigningCocah = new StudentSigningCocah();
        studentSigningCocah.setSignStudentId(Integer.parseInt(loginStudentId));
        studentSigningCocah.setSignCoachId(Integer.parseInt(coachId));
        studentSigningCocah.setSignAgreementTime(LocalDateTime.now());
        studentSigningCocah.setAgreementState(0);

        studentSigningCocahService.save(studentSigningCocah);
//        } catch (Exception e) {
//            return Result.getInstance().setMsg("签约失败").setCode(500);
//        }


        return Result.getInstance().setMsg("签约成功").setCode(200);
    }


}

